﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using FingerTracking;
using fingerWinForm.DataBase;
using fingerWinForm.Excel;

namespace fingerWinForm.GUI
{
    public partial class LoginWindow : Form
    {
        FileMgr fileManager;
        public LoginWindow()
        {
            InitializeComponent();
            if (!File.Exists(MainWindow.startingPath + "Users.xml"))
            {
                createXmlUsersFile();
            }
        }

        public void createXmlUsersFile()
        {
            StreamWriter writer = new StreamWriter(MainWindow.startingPath + "Users.xml");
            writer.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            writer.WriteLine("<users>");
            writer.WriteLine("\t<user>");  
            writer.WriteLine("\t\t<username>admin</username>");    
            writer.WriteLine("\t\t<password>admin</password>");
            writer.WriteLine("\t\t<role>Administrator</role>");
            writer.WriteLine("\t</user>");
            writer.WriteLine("</users>");
            writer.Close();
        }

        private void loginButton_Click(object sender, EventArgs e)
        {
            // Instantiate a custom Users class

            Users objUser = new Users();
            GenericPrincipal GenPrincipal;
            string strName = userNameTextBox.Text;
            string strPassword = passwordTextBox.Text;
            // Check that the login exists

            if (objUser.IsLogin(strName, strPassword))
            {
                GenPrincipal = objUser.GetLogin(strName, strPassword);
                Thread.CurrentPrincipal = GenPrincipal;

                fileManager = new FileMgr();
                fileManager.createDirectoryTree(fileManager);

                Hide();
                MainWindow mainWindow = new MainWindow(ref fileManager);
                mainWindow.ShowDialog();
            }
            else
            {
                MessageBox.Show("User Name not found.  Please try again", this.Text, MessageBoxButtons.OK,
                                MessageBoxIcon.Exclamation);

            }
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
